#include "data_global.h"
#define  N 64
//static char center_number[64];
//static char to_number[64];
/*温度过高！  unicode    \u6e29\u5ea6\u8fc7\u9ad8\uff01 
 *湿度过高！  unicode    \u6e7f\u5ea6\u8fc7\u9ad8\uff01
 *关照过高！  unicode    \u5173\u7167\u8fc7\u9ad8\uff01
 *不明身份！  unicode    \u4e0d\u660e\u8eab\u4efd\uff01
 *仓库2       unicode    4ED35E930032
 *仓库1       unicode    4ED35E930031
 * */
extern char  message[128];
extern int SMS_MSG;
extern pthread_mutex_t mutex_sms;
extern pthread_cond_t cond_sms;
extern int dev_sms_fd;
extern unsigned char dev_sms_mask;
extern char receive_phone[64];
extern char center_phone[64];
void gprs_serial_init(int fd)
{
	struct termios options;
	tcgetattr(fd,&options);  //获取属性(termios 结构)
	options.c_cflag |= (CLOCAL | CREAD); //忽略控制线，设置接收字符
	options.c_cflag &= ~CSIZE;             //传输字符的位数
	options.c_cflag &= ~CRTSCTS;           //使用硬件流控制
	options.c_cflag &= ~CSTOPB; 			//设置一位的停止位
	options.c_cflag |= CS8; 				//每个字符8位
	options.c_iflag |= IGNPAR; 				//忽略寄偶校验错误
	options.c_oflag = 0; 					//输出模式
	options.c_lflag = 0; 					//本地模式

	if(cfsetispeed(&options,B9600) == -1)    //设置输入波特率9600
	{
		printf("cfsetispeed is error\n");
		exit(1);
	}
	tcsetattr(fd,TCSANOW,&options);   		//激活新配置
	printf("init ok\n");
}
int send_message_tar(int fd, int which)     //该函数是将要发送的消息内容以及消息的格式进行整合成
{
	switch(which)
	{	
	case SMS_MSG_TEM1:
		bzero(message,sizeof(message));
		strcpy(message,center_phone);
		strcat(message,receive_phone);
		strcat(message,"104ED35E9300316E295EA68FC79AD8FF01\x1a");
		break;
	case SMS_MSG_BRE1:
		bzero(message,sizeof(message));
		strcpy(message,center_phone);
		strcat(message,receive_phone);
		strcat(message,"104ED35E9300316E7f5EA68FC79AD8FF01\x1a");
		break;
	case SMS_MSG_ILL1:
		bzero(message,sizeof(message));
		strcpy(message,center_phone);
		strcat(message,receive_phone);
		strcat(message,"104ED35E930031514971678FC79AD8FF01\x1a");
		break;
	case SMS_MSG_HUM1:
		bzero(message,sizeof(message));
		strcpy(message,center_phone);
		strcat(message,receive_phone);
		strcat(message,"104ED35E9300314E0D660E8EAb4EFDFF01\x1a");
		break;

	case SMS_MSG_TEM2:
		bzero(message,sizeof(message));
		strcpy(message,center_phone);
		strcat(message,receive_phone);
		strcat(message,"104ED35E9300326E295EA68FC79AD8FF01\x1a");
		break;
	case SMS_MSG_BRE2:
		bzero(message,sizeof(message));
		strcpy(message,center_phone);
		strcat(message,receive_phone);
		strcat(message,"104ED35E9300326E7f5EA68FC79AD8FF01\x1a");
		break;
	case SMS_MSG_ILL2:
		bzero(message,sizeof(message));
		strcpy(message,center_phone);
		strcat(message,receive_phone);
		strcat(message,"104ED35E930032514971678FC79AD8FF01\x1a");
		break;
	case SMS_MSG_HUM2:
		bzero(message,sizeof(message));
		strcpy(message,center_phone);
		strcat(message,receive_phone);
		strcat(message,"104ED35E9300324E0D660E8EAb4EFDFF01\x1a");
		break;
	}
	return 0;
}

int send_message(int dev_sms_fd, int which)  //该函数是将要发送的消息发送出去的执行过程
{

	switch(which)
	{
	case SMS_MSG_TEM1:
		send_message_tar(dev_sms_fd,SMS_MSG_TEM1);
		break;
	case SMS_MSG_BRE1:
		send_message_tar(dev_sms_fd,SMS_MSG_BRE1);
		break;
	case SMS_MSG_ILL1:
		send_message_tar(dev_sms_fd,SMS_MSG_ILL1);
		break;
	case SMS_MSG_HUM1:
		send_message_tar(dev_sms_fd,SMS_MSG_HUM1);
		break;

	case SMS_MSG_TEM2:
		send_message_tar(dev_sms_fd,SMS_MSG_TEM2);
		break;
	case SMS_MSG_BRE2:
		send_message_tar(dev_sms_fd,SMS_MSG_BRE2);
		break;
	case SMS_MSG_ILL2:
		send_message_tar(dev_sms_fd,SMS_MSG_ILL2);
		break;
	case SMS_MSG_HUM2:
		send_message_tar(dev_sms_fd,SMS_MSG_HUM2);
		break;
	}
	return 0;
}
#if 1
void set_phone_number(char from[], char to[]) //该函数是设置中心号码以及接收信息的号码
{
	bzero(receive_phone,sizeof(receive_phone));
	bzero(center_phone,sizeof(center_phone));
	int i = 0;
	char send[N] = "0891"; 
	char recv[N] = "11000D91";
	int temp;
	strcat(from,"F");
	strcat(to,"F");

	//中心号码处理
	for(i = 0;i < strlen(from);)
	{
		temp = from[i];
		from[i] = from[i+1];
		from[i+1] = temp;
		i += 2;
	}
	strcpy(center_phone,send);            //中心号码
	strcat(center_phone,from);

	//用户电话号码处理
	for(i = 0;i < strlen(to);)
	{
		temp = to[i];
		to[i] = to[i+1];
		to[i+1] = temp;
		i += 2;
	}
	strcat(recv,to);
	strcat(recv,"000800");
	strcpy(receive_phone,recv);            //用户电话号码
	puts(receive_phone);
	puts(center_phone);
}
#endif
//消息线程
void *pthread_sms(void *a)
{
	puts("pthread_sms ok!!!");
	while(1)
	{
		pthread_mutex_lock(&mutex_sms);
		int ret = 0;
		int wrt;
		char center[] = "8613800230500";
		char phone[] = "8613212582416";
		char AT[N] = "AT\r";                            //测试
		char AT_CMGF[N] = "AT+CMGF=0\r"; 				//pdu模式
		int msg_len;  									//消息长度
		char AT_CMGS[N] = {0};
		char AT_reply[N] = {0};              		     //接收返回的信息
		pthread_cond_wait(&cond_sms,&mutex_sms);
		dev_sms_fd = open(DEV_GPRS,O_RDWR | O_NOCTTY | O_NDELAY);            // /dev/ttyUSB1
		if(dev_sms_fd == -1)
		{
			printf("open DEV_GPRS error\n");
			exit(1);
		}
		gprs_serial_init(dev_sms_fd);
#if 1
		//判断SMS模块是否正常AT -->OK
		if((wrt = write(dev_sms_fd,AT,strlen(AT))) == -1)
		{
			perror("failed to AT");
			pthread_mutex_unlock(&mutex_sms);
			close(dev_sms_fd);
			continue;
		}
		ret = read(dev_sms_fd,AT_reply,wrt);
		if(strcmp(AT_reply,"ERROR") == 0 || ret == -1)
		{
			printf("AT ERROR\n");
			pthread_mutex_unlock(&mutex_sms);
			close(dev_sms_fd);
			continue;
		}
		printf("AT OK\n");
		sleep(1);
#endif
#if 1
		set_phone_number(center,phone);
		puts("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
		puts("set_phone_number ok ");
#endif
#if 1
		//传模式
		if((wrt = write(dev_sms_fd,AT_CMGF,strlen(AT_CMGF))) == -1)
		{
			perror("model send error");
			pthread_mutex_unlock(&mutex_sms);
			close(dev_sms_fd);
			continue;
		}
		printf("AT_CMGF=0 send ok\n");
		sleep(1);
#endif 

		//传消息
#if 0
		SMS_MSG = SMS_MSG_TEM1;
		printf("SMS_MSG = %d\n",SMS_MSG);
#endif
		switch(SMS_MSG)
		{
		case SMS_MSG_TEM1:
			send_message(dev_sms_fd,SMS_MSG_TEM1);
			msg_len = strlen(message) - strlen(center_phone);

			sprintf(AT_CMGS,"AT+CMGS=%d\r",msg_len / 2);          //AT+CMGS=msg_len
			if(write(dev_sms_fd,AT_CMGS,strlen(AT_CMGS)) == -1)
			{
				perror("CMGS send error");
				pthread_mutex_unlock(&mutex_sms);
				close(dev_sms_fd);
				continue;
			}
			printf("AT_CMGS send ok\n");
			sleep(1);
			if(write(dev_sms_fd,message,strlen(message)) == -1)
			{
				perror("msg send error");
				pthread_mutex_unlock(&mutex_sms);
				close(dev_sms_fd);
				continue;
			}
			break;

		case SMS_MSG_BRE1:
			send_message(dev_sms_fd,SMS_MSG_BRE1);		
			msg_len = strlen(message) - strlen(center_phone);

			sprintf(AT_CMGS,"AT+CMGS=%d\r",msg_len / 2);          //AT+CMGS=msg_len
			if(write(dev_sms_fd,AT_CMGS,strlen(AT_CMGS)) == -1)
			{
				perror("CMGS send error");
				pthread_mutex_unlock(&mutex_sms);
				close(dev_sms_fd);
				continue;
			}
			printf("AT_CMGS send ok\n");
			sleep(1);
			if(write(dev_sms_fd,message,strlen(message)) == -1)
			{
				perror("msg send error");
				pthread_mutex_unlock(&mutex_sms);
				close(dev_sms_fd);
				continue;
			}
			break;

		case SMS_MSG_ILL1:
			send_message(dev_sms_fd,SMS_MSG_ILL1);	
			msg_len = strlen(message) - strlen(center_phone);

			sprintf(AT_CMGS,"AT+CMGS=%d\r",msg_len / 2);          //AT+CMGS=msg_len
			if(write(dev_sms_fd,AT_CMGS,strlen(AT_CMGS)) == -1)
			{
				perror("CMGS send error");
				pthread_mutex_unlock(&mutex_sms);
				close(dev_sms_fd);
				continue;
			}
			printf("AT_CMGS send ok\n");
			sleep(1);
			if(write(dev_sms_fd,message,strlen(message)) == -1)
			{
				perror("msg send error");
				pthread_mutex_unlock(&mutex_sms);
				close(dev_sms_fd);
				continue;
			}
			break;
		case SMS_MSG_HUM1:
			send_message(dev_sms_fd,SMS_MSG_HUM1);			
			msg_len = strlen(message) - strlen(center_phone);

			sprintf(AT_CMGS,"AT+CMGS=%d\r",msg_len / 2);          //AT+CMGS=msg_len
			if(write(dev_sms_fd,AT_CMGS,strlen(AT_CMGS)) == -1)
			{
				perror("CMGS send error");
				pthread_mutex_unlock(&mutex_sms);
				close(dev_sms_fd);
				continue;
			}
			printf("AT_CMGS send ok\n");
			sleep(1);
			if(write(dev_sms_fd,message,strlen(message)) == -1)
			{
				perror("msg send error");
				pthread_mutex_unlock(&mutex_sms);
				close(dev_sms_fd);
				continue;
			}
			break;

		case SMS_MSG_TEM2:
			send_message(dev_sms_fd,SMS_MSG_TEM2);			
			msg_len = strlen(message) - strlen(center_phone);

			sprintf(AT_CMGS,"AT+CMGS=%d\r",msg_len / 2);          //AT+CMGS=msg_len
			if(write(dev_sms_fd,AT_CMGS,strlen(AT_CMGS)) == -1)
			{
				perror("CMGS send error");
				pthread_mutex_unlock(&mutex_sms);
				close(dev_sms_fd);
				continue;
			}
			printf("AT_CMGS send ok\n");
			sleep(1);
			if(write(dev_sms_fd,message,strlen(message)) == -1)
			{
				perror("msg send error");
				pthread_mutex_unlock(&mutex_sms);
				close(dev_sms_fd);
				continue;
			}
			break;
		case SMS_MSG_BRE2:
			send_message(dev_sms_fd,SMS_MSG_BRE2);
			msg_len = strlen(message) - strlen(center_phone);

			sprintf(AT_CMGS,"AT+CMGS=%d\r",msg_len / 2);          //AT+CMGS=msg_len
			if(write(dev_sms_fd,AT_CMGS,strlen(AT_CMGS)) == -1)
			{
				perror("CMGS send error");
				pthread_mutex_unlock(&mutex_sms);
				close(dev_sms_fd);
				continue;
			}
			printf("AT_CMGS send ok\n");
			sleep(1);
			if(write(dev_sms_fd,message,strlen(message)) == -1)
			{
				perror("msg send error");
				pthread_mutex_unlock(&mutex_sms);
				close(dev_sms_fd);
				continue;
			}
			break;
		case SMS_MSG_ILL2:
			send_message(dev_sms_fd,SMS_MSG_ILL2);	
			msg_len = strlen(message) - strlen(center_phone);

			sprintf(AT_CMGS,"AT+CMGS=%d\r",msg_len / 2);          //AT+CMGS=msg_len
			if(write(dev_sms_fd,AT_CMGS,strlen(AT_CMGS)) == -1)
			{
				perror("CMGS send error");
				pthread_mutex_unlock(&mutex_sms);
				close(dev_sms_fd);
				continue;
			}
			printf("AT_CMGS send ok\n");
			sleep(1);
			if(write(dev_sms_fd,message,strlen(message)) == -1)
			{
				perror("msg send error");
				pthread_mutex_unlock(&mutex_sms);
				close(dev_sms_fd);
				continue;
			}
			break;
		case SMS_MSG_HUM2:
			send_message(dev_sms_fd,SMS_MSG_HUM2);
			msg_len = strlen(message) - strlen(center_phone);

			sprintf(AT_CMGS,"AT+CMGS=%d\r",msg_len / 2);          //AT+CMGS=msg_len
			if(write(dev_sms_fd,AT_CMGS,strlen(AT_CMGS)) == -1)
			{
				perror("CMGS send error");
				pthread_mutex_unlock(&mutex_sms);
				close(dev_sms_fd);
				continue;
			}
			printf("AT_CMGS send ok\n");
			sleep(1);
			if(write(dev_sms_fd,message,strlen(message)) == -1)
			{
				perror("msg send error");
				pthread_mutex_unlock(&mutex_sms);
				close(dev_sms_fd);
				continue;
			}	
			break;
		}
		pthread_mutex_unlock(&mutex_sms);
		close(dev_sms_fd);
	}
	return NULL;
}
#if 0
int main(int argc, const char *argv[])
{
	pthread_t sms_pthread;
	//创建线程
	pthread_create(&sms_pthread,NULL,pthread_sms,NULL);
	pthread_join(sms_pthread,NULL);
	pthread_mutex_destroy(&mutex_sms);
	pthread_cond_destroy(&cond_sms);
	return 0;
}
#endif
